package com.eesujie.xcs.common;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Getter;
import lombok.Setter;

@Setter
@Getter
public class Result {
    public enum ResultStatusEnum {
        SUCCESS(200, "ok"),
        BAD_REQUEST(400, "bad request"),
        UNAUTHORIZED(401, "未登录"),
        Forbidden(401, "权限不够"),
        ;
        private final int code;
        private final String message;
        ResultStatusEnum(int code, String message) {
            this.code = code;
            this.message = message;
        }
    }
    private final int code;
    private final String message;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    private final Object data;

    private Result(int code, String message, Object data){
        this.code = code;
        this.message = message;
        this.data = data;
    }
    private Result(ResultStatusEnum resultStatusEnum, Object data) {
        this.code = resultStatusEnum.code;
        this.message = resultStatusEnum.message;
        this.data = data;
    }
    private Result(ResultStatusEnum resultStatusEnum) {
        this(resultStatusEnum, null);
    }
    public static Result success() {
        return new Result(ResultStatusEnum.SUCCESS);
    }
    public static Result success(Object data) {
        return new Result(ResultStatusEnum.SUCCESS, data);
    }

    public static Result failure(ResultStatusEnum statusEnum) {
        return new Result(statusEnum);
    }

    public static Result failure(String message) {
        return new Result(400, message, null);
    }
}
